package com.taobao.api;

import static com.taobao.api.util.ApiConstants.BANNER;
import static com.taobao.api.util.ApiConstants.FIELDS;
import static com.taobao.api.util.ApiConstants.HAS_DISCOUNT;
import static com.taobao.api.util.ApiConstants.HAS_SHOWCASE;
import static com.taobao.api.util.ApiConstants.ORDER_BY;
import static com.taobao.api.util.ApiConstants.PAGE_NO;
import static com.taobao.api.util.ApiConstants.PAGE_SIZE;
import static com.taobao.api.util.ApiConstants.Q;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.taobao.api.convert.TaobaoItemJSONConvert;
import com.taobao.api.convert.TaobaoPostageJSONConvert;
import com.taobao.api.convert.TaobaoProductJSONConvert;
import com.taobao.api.convert.TaobaoSellerJSONConvert;
import com.taobao.api.convert.TaobaoShippingJSONConvert;
import com.taobao.api.convert.TaobaoShopJSONConvert;
import com.taobao.api.convert.TaobaoSkuJSONConvert;
import com.taobao.api.convert.TaobaoSpuJSONConvert;
import com.taobao.api.convert.TaobaoTradeJSONConvert;
import com.taobao.api.convert.TaobaoUserJSONConvert;
import com.taobao.api.model.*;
import com.taobao.api.util.ApiConstants;
import com.taobao.api.util.DateUtil;

/**
 * 淘宝对外部提供的REST API客户端
 * 
 * 注意，默认是SIP入口。使用淘宝API入口需要设置：
 * 
 * <code>
 * this.useTaobaoProtocol()
 * </code>
 * 
 * @version 2008-8-28
 * @author <a href="mailto:zixue@taobao.com">zixue</a>
 * 
 */
public class TaobaoJsonRestClient extends AbstractTaobaoRestClient implements
		TaobaoRestClient {

	public TaobaoJsonRestClient(String appkey, String secret)
			throws TaobaoApiException {
		this(appkey, secret, false);
	}

	public TaobaoJsonRestClient(String appkey, String secret, boolean isSandbox)
			throws TaobaoApiException {
		this(isSandbox ? ApiConstants.SIP_SANDBOX_SERVICE_URL
				: ApiConstants.SIP_SERVICE_URL, appkey, secret);
	}

	public TaobaoJsonRestClient(String appkey, String secret,
			boolean isSandbox, boolean sipProtocol) throws TaobaoApiException {
		this(isSandbox ? (sipProtocol ? ApiConstants.SIP_SANDBOX_SERVICE_URL
				: ApiConstants.API_SANDBOX_SERVICE_URL)
				: (sipProtocol ? ApiConstants.SIP_SERVICE_URL
						: ApiConstants.API_SERVICE_URL), appkey, secret,
				sipProtocol);
	}

	public TaobaoJsonRestClient(String serviceUrl, String appkey, String secret)
			throws TaobaoApiException {
		this(serviceUrl, ApiConstants.DEFAULT_SERVICE_VERSION, appkey, secret);
	}

	public TaobaoJsonRestClient(String serviceUrl, String appkey,
			String secret, boolean sipProtocl) throws TaobaoApiException {
		this(serviceUrl, ApiConstants.DEFAULT_SERVICE_VERSION, appkey, secret,
				sipProtocl);
	}

	public TaobaoJsonRestClient(String serviceUrl, String version,
			String appkey, String secret) throws TaobaoApiException {
		this(serviceUrl, ApiConstants.DEFAULT_SERVICE_VERSION, appkey, secret,
				true);
	}

	public TaobaoJsonRestClient(String serviceUrl, String version,
			String appkey, String secret, boolean sipProtocol)
			throws TaobaoApiException {
		super(serviceUrl, version, appkey, secret);
		if (sipProtocol) {
			useSIPProtocol();
		} else {
			useTaobaoProtocol();
		}
	}

	private void useTaobaoProtocol() {
		ApiConstants.enableTaobao = true;
		ApiConstants.enableSIP = false;
	}

	private void useSIPProtocol() {
		ApiConstants.enableTaobao = false;
		ApiConstants.enableSIP = true;
	}

	protected String getApiKeyName() {
		String apiKey = ApiConstants.EMPTY_STR;
		if (ApiConstants.enableSIP) {
			apiKey = ApiConstants.SIP_APPKEY;
		} else if (ApiConstants.enableTaobao) {
			apiKey = ApiConstants.APIKEY;
		}
		return apiKey;
	}

	protected String getMethodName() {
		String method = ApiConstants.EMPTY_STR;
		if (ApiConstants.enableSIP) {
			method = ApiConstants.SIP_APINAME;
		} else if (ApiConstants.enableTaobao) {
			method = ApiConstants.METHOD;
		}
		return method;
	}

	protected String getSignName() {
		String sign = ApiConstants.EMPTY_STR;
		if (ApiConstants.enableSIP) {
			sign = ApiConstants.SIP_SIGN;
		} else if (ApiConstants.enableTaobao) {
			sign = ApiConstants.SIGN;
		}
		return sign;
	}

	protected String getSessionIdName() {
		String session = ApiConstants.EMPTY_STR;
		if (ApiConstants.enableSIP) {
			session = ApiConstants.SIP_SESSIONID;
		} else if (ApiConstants.enableTaobao) {
			session = ApiConstants.SESSION;
		}
		return session;
	}

	protected String getTimestampName() {
		String timestamp = ApiConstants.EMPTY_STR;
		if (ApiConstants.enableSIP) {
			timestamp = ApiConstants.SIP_TIMESTAMP;
		} else if (ApiConstants.enableTaobao) {
			timestamp = ApiConstants.TIMESTAMP;
		}
		return timestamp;
	}

	/**
	 * 通过系统平台（sip）的检查（签名、绑定用户）后，检测淘宝的接口是否调用正常
	 * 
	 * @param response
	 * @param json
	 * @throws JSONException
	 */
	protected void parseError(UrlResponse response, JSONObject json)
			throws JSONException {
		if (json.has(ApiConstants.ERROR_RSP)) {
			JSONObject errorRsp = json.getJSONObject(ApiConstants.ERROR_RSP);
			response.setErrorCode(errorRsp.getString(ApiConstants.ERROR_CODE));
			response.setMsg(errorRsp.getString(ApiConstants.ERROR_MSG));
		}
	}

	/**
	 * 设置返回的response中totalResults(取得到商品、交易等的 总数)对应字段的值
	 * 
	 * @param rsp
	 *            JSONObject
	 * @param listUrlResponse
	 *            返回的response对象
	 * @throws NumberFormatException
	 * @throws JSONException
	 */
	protected void setTotalResults(JSONObject rsp,
			ListUrlResponse listUrlResponse) throws NumberFormatException,
			TaobaoApiException {
		if (rsp.has(ApiConstants.TOTALRESULTS)) {
			try {
				listUrlResponse.setTotalResults(Integer.parseInt(rsp
						.getString(ApiConstants.TOTALRESULTS)));
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
	}

	protected JSONObject getRsp(JSONObject json) throws JSONException {
		return json.getJSONObject(ApiConstants.RSP);
	}

	/*------------------------------------------------------------------------------------------------------------------------------------------*/

	// 商品部分
	public ItemAddResponse itemAdd(ItemAddRequest itemAddRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareItemAdd(itemAddRequest);
		return sendItemAdd(params, itemAddRequest, sessionId);
	}

	protected Map<String, Object> prepareItemAdd(ItemAddRequest itemAddRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.VALID_THRU, itemAddRequest.getValidThru());
		params.put(ApiConstants.HAS_INVOICE, itemAddRequest.getHasInvoice());
		params.put(ApiConstants.HAS_WARRANTY, itemAddRequest.getHasWarranty());
		params.put(ApiConstants.SELLER_CIDS, itemAddRequest.getSellerCids());
		params.put(ApiConstants.HAS_DISCOUNT, itemAddRequest.getHasDiscount());
		params.put(ApiConstants.POST_FEE, itemAddRequest.getPostFee());
		params.put(ApiConstants.EXPRESS_FEE, itemAddRequest.getExpressFee());
		params.put(ApiConstants.LIST_TIME, itemAddRequest.getListTime());
		params.put(ApiConstants.INCREMENT, itemAddRequest.getIncrement());
		params.put(ApiConstants.AUTO_REPOST, itemAddRequest.getAutoRepost());
		params.put(ApiConstants.HAS_SHOWCASE, itemAddRequest.getHasShowcase());
		params.put(ApiConstants.EMS_FEE, itemAddRequest.getEmsFee());
		params.put(ApiConstants.CID, itemAddRequest.getCid());
		params.put(ApiConstants.PROPS, itemAddRequest.getProps());
		params.put(ApiConstants.NUM, itemAddRequest.getNum());
		params.put(ApiConstants.PRICE, itemAddRequest.getPrice());
		params.put(ApiConstants.TITLE, itemAddRequest.getTitle());
		params.put(ApiConstants.DESC, itemAddRequest.getDesc());
		params.put(ApiConstants.TYPE, itemAddRequest.getType());
		params.put(ApiConstants.STUFF_STATUS, itemAddRequest.getStuffStatus());
		params.put(ApiConstants.APPROVE_STATUS, itemAddRequest
				.getApproveStatus());
		params
				.put(ApiConstants.FREIGHT_PAYER, itemAddRequest
						.getFreightPayer());
		if (itemAddRequest.getLocation() != null) {
			params.put(ApiConstants.LOCATION_STATE, (itemAddRequest
					.getLocation().getState()));
			params.put(ApiConstants.LOCATION_CITY, (itemAddRequest
					.getLocation().getCity()));
		}
		params
				.put(ApiConstants.AUCTION_POINT, itemAddRequest
						.getAuctionPoint());
		params.put(ApiConstants.PROPERTY_ALIAS, itemAddRequest
				.getPropertyAlias());
		params.put(ApiConstants.SKU_PROPERTIES, itemAddRequest
				.getSkuProperties());
		params.put(ApiConstants.SKU_QUANTITIES, itemAddRequest
				.getSkuQuantities());
		params.put(ApiConstants.SKU_PRICES, itemAddRequest.getSkuPrices());
		params.put(ApiConstants.SKU_OUTER_IDS, itemAddRequest.getSkuOuterIds());
		params.put(ApiConstants.POSTAGE_ID, itemAddRequest.getPostageId());
		params.put(ApiConstants.INPUT_PIDS, itemAddRequest.getInputPids());
		params.put(ApiConstants.INPUT_STR, itemAddRequest.getInputStr());
		params.put(ApiConstants.LANG, itemAddRequest.getLang());
		return params;
	}

	protected ItemAddResponse sendItemAdd(Map<String, Object> params,
			ItemAddRequest itemAddRequest, String sessionId)
			throws TaobaoApiException {
		ItemAddResponse itemAddResponse = getItemAddResponse(itemAddRequest,
				sessionId, params);
		// 平台成功访问
		if (itemAddResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(itemAddResponse.getBody());
				parseError(itemAddResponse, json);
				// 访问淘宝成功
				if (itemAddResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						Item addedItem = new Item();
						try {
							addedItem.setModified(DateUtil.strToDate(item
									.getString(ApiConstants.CREATED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						addedItem.setIid(item.getString(ApiConstants.IID));
						itemAddResponse.setItem(addedItem);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemAddResponse;
	}

	/**
	 * 获得调用添加商品API的响应response
	 * 
	 * @param itemAddRequest
	 *            添加商品的请求
	 * @param sessionId
	 *            请求所包含的session id
	 * @param paramsMap
	 *            请求参数
	 * @return ItemAddResponse
	 * @throws TaobaoApiException
	 */
	private ItemAddResponse getItemAddResponse(ItemAddRequest itemAddRequest,
			String sessionId, Map<String, Object> paramsMap)
			throws TaobaoApiException {
		UrlResponse rsp = null;
		// 根据添加商品是否有图片选择不同的请求方法
		if (itemAddRequest.getImage() != null) {
			rsp = this.getFetch().fetchWithFile(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.ITEM_ADD.getMethod(),
							sessionId, paramsMap), itemAddRequest.getImage());
		} else {
			rsp = this.getFetch().fetch(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.ITEM_ADD.getMethod(),
							sessionId, paramsMap));
		}
		return new ItemAddResponse(rsp);
	}

	/**
	 * 获取单个商品
	 * 
	 * @param itemGetRequest
	 * @return ItemGetResponse
	 * @throws TaobaoApiException
	 */
	public ItemGetResponse itemGet(ItemGetRequest itemGetRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareItemGet(itemGetRequest);
		return sendItemGet(params, sessionId);
	}

	protected Map<String, Object> prepareItemGet(ItemGetRequest itemGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.FIELDS, itemGetRequest.getFields());
		params.put(ApiConstants.NICK, itemGetRequest.getNick());
		params.put(ApiConstants.IID, itemGetRequest.getIid());
		return params;
	}

	protected ItemGetResponse sendItemGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_GET.getMethod(),
						sessionId, params));
		ItemGetResponse itemGetResponse = new ItemGetResponse(response);
		// 平台成功访问
		if (itemGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemGetResponse.getBody());
				parseError(itemGetResponse, json);
				// 访问淘宝成功
				if (itemGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						itemGetResponse.setItem(TaobaoItemJSONConvert
								.convertJsonToItem(item));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemGetResponse;
	}

	public ItemGetResponse itemGet(ItemGetRequest itemGetRequest)
			throws TaobaoApiException {
		return this.itemGet(itemGetRequest, null);
	}

	/**
	 * 更新单个商品信息
	 * 
	 * @param itemUpdateRequest
	 * @return ItemUpdateResponse
	 * @throws TaobaoApiException
	 */
	public ItemUpdateResponse itemUpdate(ItemUpdateRequest itemUpdateRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareItemUpdate(itemUpdateRequest);
		return sendItemUpdate(itemUpdateRequest, params, sessionId);
	}

	protected Map<String, Object> prepareItemUpdate(
			ItemUpdateRequest itemUpdateRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemUpdateRequest请求中的参数添加到params
		params.put(ApiConstants.IID, itemUpdateRequest.getIid());
		params.put(ApiConstants.CID, itemUpdateRequest.getCid());
		params.put(ApiConstants.PROPS, itemUpdateRequest.getProps());
		params.put(ApiConstants.NUM, itemUpdateRequest.getNum());
		params.put(ApiConstants.PRICE, itemUpdateRequest.getPrice());
		params.put(ApiConstants.STUFF_STATUS, itemUpdateRequest
				.getStuffStatus());
		params.put(ApiConstants.TITLE, itemUpdateRequest.getTitle());
		params.put(ApiConstants.DESC, itemUpdateRequest.getDesc());
		params.put(ApiConstants.VALID_THRU, itemUpdateRequest.getValidThru());
		params.put(ApiConstants.HAS_INVOICE, itemUpdateRequest.getHasInvoice());
		params.put(ApiConstants.HAS_WARRANTY, itemUpdateRequest
				.getHasWarranty());
		params.put(ApiConstants.SELLER_CIDS, itemUpdateRequest.getSellerCids());
		params.put(ApiConstants.HAS_DISCOUNT, itemUpdateRequest
				.getHasDiscount());
		params.put(ApiConstants.POST_FEE, itemUpdateRequest.getPostFee());
		params.put(ApiConstants.EXPRESS_FEE, itemUpdateRequest.getExpressFee());
		params.put(ApiConstants.LIST_TIME, itemUpdateRequest.getListTime());
		params.put(ApiConstants.INCREMENT, itemUpdateRequest.getIncrement());
		params.put(ApiConstants.AUTO_REPOST, itemUpdateRequest.getAutoRepost());
		params.put(ApiConstants.HAS_SHOWCASE, itemUpdateRequest
				.getHasShowcase());
		params.put(ApiConstants.EMS_FEE, itemUpdateRequest.getEmsFee());
		params.put(ApiConstants.APPROVE_STATUS, itemUpdateRequest
				.getApproveStatus());
		params.put(ApiConstants.FREIGHT_PAYER, itemUpdateRequest
				.getFreightPayer());
		if (itemUpdateRequest.getLocation() != null) {
			params.put(ApiConstants.LOCATION_STATE, (itemUpdateRequest
					.getLocation().getState()));
			params.put(ApiConstants.LOCATION_CITY, (itemUpdateRequest
					.getLocation().getCity()));
		}
		params.put(ApiConstants.AUCTION_POINT, itemUpdateRequest
				.getAuctionPoint());
		params.put(ApiConstants.PROPERTY_ALIAS, itemUpdateRequest
				.getPropertyAlias());
		params.put(ApiConstants.SKU_IDS, itemUpdateRequest.getSkuIds());
		params.put(ApiConstants.SKU_PROPERTIES, itemUpdateRequest
				.getSkuProperties());
		params.put(ApiConstants.SKU_QUANTITIES, itemUpdateRequest
				.getSkuQuantities());
		params.put(ApiConstants.SKU_PRICES, itemUpdateRequest.getSkuPrices());
		params.put(ApiConstants.SKU_OUTER_IDS, itemUpdateRequest
				.getSkuOuterIds());
		params.put(ApiConstants.POSTAGE_ID, itemUpdateRequest.getPostageId());
		params.put(ApiConstants.INPUT_PIDS, itemUpdateRequest.getInputPids());
		params.put(ApiConstants.INPUT_STR, itemUpdateRequest.getInputStr());
		params.put(ApiConstants.LANG, itemUpdateRequest.getLang());
		return params;
	}

	protected ItemUpdateResponse sendItemUpdate(
			ItemUpdateRequest itemUpdateRequest, Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		// 请求调用更新商品API，返回 响应的response
		ItemUpdateResponse itemUpdateResponse = getUpdateResponse(
				itemUpdateRequest, sessionId, params);
		// 平台成功访问
		if (itemUpdateResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(itemUpdateResponse.getBody());
				parseError(itemUpdateResponse, json);
				// 访问淘宝成功
				if (itemUpdateResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						Item updatedItem = new Item();
						try {
							updatedItem.setModified(DateUtil.strToDate(item
									.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						updatedItem.setIid(item.getString(ApiConstants.IID));
						itemUpdateResponse.setItem(updatedItem);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemUpdateResponse;
	}

	/**
	 * 获得更新商品API的相应response
	 * 
	 * @param req
	 *            更新商品的请求
	 * @param sessionId
	 *            请求中包含的session id
	 * @param params
	 *            请求参数
	 * @return
	 * @throws TaobaoApiException
	 */
	private ItemUpdateResponse getUpdateResponse(ItemUpdateRequest req,
			String sessionId, Map<String, Object> params)
			throws TaobaoApiException {
		UrlResponse rsp = null;
		// 根据添加商品是否有图片选择不同的请求方法
		if (req.getImage() != null) {
			rsp = this.getFetch().fetchWithFile(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.ITEM_UPDATE.getMethod(),
							sessionId, params), req.getImage());
		} else {
			rsp = this.getFetch().fetch(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.ITEM_UPDATE.getMethod(),
							sessionId, params));
		}
		return new ItemUpdateResponse(rsp);
	}

	/**
	 * 批量获取商品信息
	 * 
	 * @param itemUpdateRequest
	 * @return ItemUpdateResponse
	 * @throws TaobaoApiException
	 */
	public ItemsGetResponse itemsGet(ItemsGetRequest itemsGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemsGetRequest请求中的参数添加到params
		params.put(ApiConstants.Q, itemsGetRequest.getQ());
		params.put(ApiConstants.FIELDS, itemsGetRequest.getFields());
		params.put(ApiConstants.START_PRICE, itemsGetRequest.getStartPrice());
		params.put(ApiConstants.END_PRICE, itemsGetRequest.getEndPrice());
		params.put(ApiConstants.CID, itemsGetRequest.getCid());
		params.put(ApiConstants.PAGE_NO, itemsGetRequest.getPageNo());
		params.put(ApiConstants.PAGE_SIZE, itemsGetRequest.getPageSize());
		params.put(ApiConstants.ORDER_BY, itemsGetRequest.getOrderBy());
		params.put(ApiConstants.NICKS, itemsGetRequest.getNicks());
		params.put(ApiConstants.PROPS, itemsGetRequest.getProps());
		params.put(ApiConstants.PRODUCT_ID, itemsGetRequest.getProductId());
		params.put(ApiConstants.WW_STATUS, itemsGetRequest.getWwStatus());
		params.put(ApiConstants.POST_FREE, itemsGetRequest.getPostFree());
		params.put(ApiConstants.LOCATION_CITY, itemsGetRequest.getCity());
		params.put(ApiConstants.LOCATION_STATE, itemsGetRequest.getState());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMS_GET.getMethod(),
						params));
		ItemsGetResponse itemsGetResponse = new ItemsGetResponse(response);
		// 平台访问成功
		if (itemsGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemsGetResponse.getBody());
				parseError(itemsGetResponse, json);
				// 访问淘宝成功
				if (itemsGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						itemsGetResponse.setItems(TaobaoItemJSONConvert
								.convertJsonArrayToItemList(items));
					}
					setTotalResults(rsp, itemsGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemsGetResponse;
	}

	/**
	 * 得到当前会话用户（做为卖家）的出售中商品列表
	 * 
	 * @param itemsOnSaleGetRequest
	 * @return ItemsGetResponse
	 * @throws TaobaoApiException
	 */
	public ItemsGetResponse itemsOnSaleGet(
			ItemsOnSaleGetRequest itemsOnSaleGetRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemsOnSaleGet(itemsOnSaleGetRequest);
		return sendItemsOnSaleGet(params, sessionId);
	}

	/**
	 * 数据准备，将itemsOnSaleRequest数据封装为Map对象返回
	 * 
	 * @param ItemStatsGetRequest
	 * @return Map<String, Object>
	 * @throws
	 */
	protected Map<String, Object> prepareItemsOnSaleGet(
			ItemsOnSaleGetRequest itemsOnSaleGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(FIELDS, itemsOnSaleGetRequest.getFields());
		params.put(HAS_DISCOUNT, itemsOnSaleGetRequest.getHasDiscount());
		params.put(HAS_SHOWCASE, itemsOnSaleGetRequest.getHasShowcase());
		params.put(Q, itemsOnSaleGetRequest.getQ());
		params.put(PAGE_NO, itemsOnSaleGetRequest.getPageNo());
		params.put(PAGE_SIZE, itemsOnSaleGetRequest.getPageSize());
		params.put(ORDER_BY, itemsOnSaleGetRequest.getOrderBy());
		return params;
	}

	protected ItemsGetResponse sendItemsOnSaleGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(
						TaobaoApiMethod.ITEMS_ONSALE_GET.getMethod(),
						sessionId, params));
		ItemsGetResponse itemsOnSaleGetResponse = new ItemsGetResponse(response);
		// 平台访问成功
		if (itemsOnSaleGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemsOnSaleGetResponse.getBody());
				parseError(itemsOnSaleGetResponse, json);
				// 访问淘宝成功
				if (itemsOnSaleGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						itemsOnSaleGetResponse.setItems(TaobaoItemJSONConvert
								.convertJsonArrayToItemList(items));
					}
					setTotalResults(rsp, itemsOnSaleGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemsOnSaleGetResponse;
	}

	/**
	 * 数据准备，将itemsInStockRequest数据封装为Map对象返回
	 * 
	 * @param ItemStatsGetRequest
	 * @return Map<String, Object>
	 * @throws
	 */
	protected Map<String, Object> prepareItemsInStockGet(
			ItemsInStockGetRequest itemsInStockGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(FIELDS, itemsInStockGetRequest.getFields());
		params.put(BANNER, itemsInStockGetRequest.getBanner());
		params.put(Q, itemsInStockGetRequest.getQ());
		params.put(PAGE_NO, itemsInStockGetRequest.getPageNo());
		params.put(PAGE_SIZE, itemsInStockGetRequest.getPageSize());
		params.put(ORDER_BY, itemsInStockGetRequest.getOrderBy());
		return params;
	}

	protected ItemsGetResponse sendItemsInStockGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMS_INSTOCK_GET
						.getMethod(), sessionId, params));
		ItemsGetResponse itemsInStockGetResponse = new ItemsGetResponse(
				response);
		// 平台访问成功
		if (itemsInStockGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemsInStockGetResponse.getBody());
				parseError(itemsInStockGetResponse, json);
				// 淘宝访问成功
				if (itemsInStockGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						itemsInStockGetResponse.setItems(TaobaoItemJSONConvert
								.convertJsonArrayToItemList(items));
					}
					setTotalResults(rsp, itemsInStockGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemsInStockGetResponse;
	}

	/**
	 * 搜索当前会话用户（做为卖家）的库存商品
	 * 
	 * @param itemsInStockGetRequest
	 * @return ItemsGetResponse
	 * @throws TaobaoApiException
	 */
	public ItemsGetResponse itemsInStockGet(
			ItemsInStockGetRequest itemsInStockGetRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemsInStockGet(itemsInStockGetRequest);
		return sendItemsInStockGet(params, sessionId);
	}

	/**
	 * 此接口用于商品下架
	 * 
	 * @param itemUpdateDelistingRequest
	 * @return ItemUpdateDelistingResponse
	 * @throws TaobaoApiException
	 */
	public ItemUpdateDelistingResponse itemUpdateDelisting(
			ItemUpdateDelistingRequest itemUpdateDelistingRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareItemDelisting(itemUpdateDelistingRequest);
		return sendItemUpdateDelisting(params, sessionId);
	}

	protected Map<String, Object> prepareItemDelisting(
			ItemUpdateDelistingRequest itemUpdateDelistingRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemUpdateDelisting请求中的参数添加到params
		params.put(ApiConstants.IID, itemUpdateDelistingRequest.getIid());
		return params;
	}

	protected ItemUpdateDelistingResponse sendItemUpdateDelisting(
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_UPDATE_DELISTING
						.getMethod(), sessionId, params));
		ItemUpdateDelistingResponse itemUpdateDelistingResponse = new ItemUpdateDelistingResponse(
				response);
		// 平台成功访问
		if (itemUpdateDelistingResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(itemUpdateDelistingResponse
						.getBody());
				parseError(itemUpdateDelistingResponse, json);
				// 访问淘宝成功
				if (itemUpdateDelistingResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						itemUpdateDelistingResponse.setIid(item
								.getString(ApiConstants.IID));
						try {
							itemUpdateDelistingResponse.setModified(DateUtil
									.strToDate(item
											.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemUpdateDelistingResponse;
	}

	/**
	 * 此接口方法用以一口价商品的上架
	 * 
	 * @param itemUpdateListingRequest
	 * @return ItemUpdateListingResponse
	 * @throws TaobaoApiException
	 */
	public ItemUpdateListingResponse itemUpdateListing(
			ItemUpdateListingRequest itemUpdateListingRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemListing(itemUpdateListingRequest);
		return sendItemUpdateListing(params, sessionId);
	}

	protected Map<String, Object> prepareItemListing(
			ItemUpdateListingRequest itemUpdateListingRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemUpdateListing请求中的参数添加到params
		params.put(ApiConstants.IID, itemUpdateListingRequest.getIid());
		params.put(ApiConstants.NUM, itemUpdateListingRequest.getNum());
		return params;
	}

	protected ItemUpdateListingResponse sendItemUpdateListing(
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_UPDATE_LISTING
						.getMethod(), sessionId, params));
		ItemUpdateListingResponse itemUpdateListingResponse = new ItemUpdateListingResponse(
				response);
		// 平台成功访问
		if (itemUpdateListingResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(itemUpdateListingResponse
						.getBody());
				parseError(itemUpdateListingResponse, json);
				// 访问淘宝成功
				if (itemUpdateListingResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						itemUpdateListingResponse.setIid(item
								.getString(ApiConstants.IID));
						try {
							itemUpdateListingResponse.setModified(DateUtil
									.strToDate(item
											.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemUpdateListingResponse;
	}

	/**
	 * 此接口用于取消商品的橱窗推荐
	 * 
	 * @param itemUpdateRevokeShowcaseRequest
	 * @return ItemUpdateRevokeShowcaseResponse
	 * @throws TaobaoApiException
	 */
	public ItemUpdateRevokeShowcaseResponse itemUpdateRevokeShowcase(
			ItemUpdateRevokeShowcaseRequest itemUpdateRevokeShowcaseRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareItemRevokeShowcase(itemUpdateRevokeShowcaseRequest);
		return sendItemRevokeShowcase(params, sessionId);
	}

	protected Map<String, Object> prepareItemRevokeShowcase(
			ItemUpdateRevokeShowcaseRequest itemUpdateRevokeShowcaseRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemUpdateRevokeShowcase请求中的参数添加到params
		params.put(ApiConstants.IID, itemUpdateRevokeShowcaseRequest.getIid());
		return params;
	}

	protected ItemUpdateRevokeShowcaseResponse sendItemRevokeShowcase(
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_UPDATE_REVOKESHOWCASE
						.getMethod(), sessionId, params));
		ItemUpdateRevokeShowcaseResponse itemUpdateRevokeShowcaseResponse = new ItemUpdateRevokeShowcaseResponse(
				response);
		// 平台成功访问
		if (itemUpdateRevokeShowcaseResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(
						itemUpdateRevokeShowcaseResponse.getBody());
				parseError(itemUpdateRevokeShowcaseResponse, json);
				// 访问淘宝成功
				if (itemUpdateRevokeShowcaseResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						itemUpdateRevokeShowcaseResponse.setIid(item
								.getString(ApiConstants.IID));
						try {
							itemUpdateRevokeShowcaseResponse
									.setModified(DateUtil.strToDate(item
											.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemUpdateRevokeShowcaseResponse;
	}

	/**
	 * 此接口用于商品加入橱窗推荐
	 * 
	 * @param itemUpdateShowcaseRequest
	 * @return ItemUpdateShowcaseResponse
	 * @throws TaobaoApiException
	 */
	public ItemUpdateShowcaseResponse itemUpdateShowcase(
			ItemUpdateShowcaseRequest itemUpdateShowcaseRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareItemShowcase(itemUpdateShowcaseRequest);
		return sendItemShowcase(params, sessionId);
	}

	protected Map<String, Object> prepareItemShowcase(
			ItemUpdateShowcaseRequest itemUpdateShowcaseRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemUpdateShowcase请求中的参数添加到params
		params.put(ApiConstants.IID, itemUpdateShowcaseRequest.getIid());
		return params;
	}

	protected ItemUpdateShowcaseResponse sendItemShowcase(
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_UPDATE_SHOWCASE
						.getMethod(), sessionId, params));
		ItemUpdateShowcaseResponse itemUpdateShowcaseResponse = new ItemUpdateShowcaseResponse(
				response);
		// 平台成功访问
		if (itemUpdateShowcaseResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(itemUpdateShowcaseResponse
						.getBody());
				parseError(itemUpdateShowcaseResponse, json);
				// 访问淘宝成功
				if (itemUpdateShowcaseResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						itemUpdateShowcaseResponse.setIid(item
								.getString(ApiConstants.IID));
						try {
							itemUpdateShowcaseResponse.setModified(DateUtil
									.strToDate(item
											.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemUpdateShowcaseResponse;
	}

	public ItemCatsResponse itemCatsGetV2(
			ItemCatsGetV2Request itemCatsGetV2Request)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemCatsGetV2(itemCatsGetV2Request);
		return sendItemCatsGetV2(params);
	}

	protected Map<String, Object> prepareItemCatsGetV2(
			ItemCatsGetV2Request itemCatsGetV2Request) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.FIELDS, itemCatsGetV2Request.getFields());
		params
				.put(ApiConstants.PARENT_CID, itemCatsGetV2Request
						.getParentCid());
		params.put(ApiConstants.CIDS, itemCatsGetV2Request.getCids());
		params.put(ApiConstants.DATETIME, itemCatsGetV2Request.getDatetime());
		return params;
	}

	protected ItemCatsResponse sendItemCatsGetV2(Map<String, Object> params)
			throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMCATS_GET_V2.getMethod(),
						params));
		ItemCatsResponse itemCatsGetResponse = new ItemCatsResponse(response);
		// 平台访问成功
		if (itemCatsGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemCatsGetResponse.getBody());
				parseError(itemCatsGetResponse, json);
				// 淘宝访问成功
				if (itemCatsGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEM_CATS)) {
						JSONArray itemcats = rsp
								.getJSONArray(ApiConstants.ITEM_CATS);
						itemCatsGetResponse.setItemCats(TaobaoItemJSONConvert
								.convertJsonArrayToItemCatList(itemcats));
					}
					setTotalResults(rsp, itemCatsGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}

		}
		return itemCatsGetResponse;
	}

	public ItemPropsResponse itemPropsGetV2(
			ItemPropsV2Request itemPropsV2Request) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemPropsGetRequest请求中的参数添加到params
		params.put(ApiConstants.CID, itemPropsV2Request.getCid());
		params.put(ApiConstants.FIELDS, itemPropsV2Request.getFields());
		params.put(ApiConstants.PID, itemPropsV2Request.getPid());
		params.put(ApiConstants.PARENT_PID, itemPropsV2Request.getParentPid());
		params.put(ApiConstants.IS_KEY_PROP, itemPropsV2Request.getIsKeyProp());
		params.put(ApiConstants.IS_SALE_PROP, itemPropsV2Request
				.getIsSaleProp());
		params.put(ApiConstants.IS_COLOR_PROP, itemPropsV2Request
				.getIsColorProp());
		params.put(ApiConstants.IS_ENUM_PROP, itemPropsV2Request
				.getIsEnumProp());
		params.put(ApiConstants.IS_INPUT_PROP, itemPropsV2Request
				.getIsInputProp());
		params.put(ApiConstants.DATETIME, itemPropsV2Request.getDatetime());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(
						TaobaoApiMethod.ITEMPROPS_GET_V2.getMethod(), params));
		ItemPropsResponse itemPropsGetResponse = new ItemPropsResponse(response);
		// 平台访问成功
		if (itemPropsGetResponse.isSuccess()) {
			JSONObject json = null;
			try {
				json = new JSONObject(itemPropsGetResponse.getBody());
				parseError(itemPropsGetResponse, json);
				// 淘宝访问成功
				if (itemPropsGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEM_PROPS)) {
						JSONArray item_props = rsp
								.getJSONArray(ApiConstants.ITEM_PROPS);
						itemPropsGetResponse.setItemProps(TaobaoItemJSONConvert
								.convertJSONArrayToItemPropList(item_props));
					}
					setTotalResults(rsp, itemPropsGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemPropsGetResponse;
	}

	public ItemPropValuesResponse itemPropValuesGet(
			ItemPropValuesGetRequest itemPropValuesGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemPropsGetRequest请求中的参数添加到params
		params.put(ApiConstants.CID, itemPropValuesGetRequest.getCid());
		params.put(ApiConstants.FIELDS, itemPropValuesGetRequest.getFields());
		params.put(ApiConstants.PVS, itemPropValuesGetRequest.getPvs());
		params.put(ApiConstants.DATETIME, itemPropValuesGetRequest
				.getDatetime());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMPROPVALUES_GET
						.getMethod(), params));
		ItemPropValuesResponse itemPropValuesResponse = new ItemPropValuesResponse(
				response);
		// 平台访问成功
		if (itemPropValuesResponse.isSuccess()) {
			JSONObject json = null;
			try {
				json = new JSONObject(itemPropValuesResponse.getBody());
				parseError(itemPropValuesResponse, json);
				// 淘宝访问成功
				if (itemPropValuesResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.PROP_VALUES)) {
						JSONArray prop_values = rsp
								.getJSONArray(ApiConstants.PROP_VALUES);
						itemPropValuesResponse
								.setPropValues(TaobaoProductJSONConvert
										.convertJsonArrayToPropValueList(prop_values));
					}
					setTotalResults(rsp, itemPropValuesResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemPropValuesResponse;
	}

	

	// *********************类目属性部分************************

	/**
	 * 获取后台商品类
	 * 
	 * @param itemCatsGetRequest
	 * @return ItemCatsResponse
	 * @throws TaobaoApiException
	 */
	public ItemCatsResponse itemCatsGet(ItemCatsGetRequest itemCatsGetRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareItemCatsGet(itemCatsGetRequest);
		return sendItemCatsGet(params, sessionId);
	}

	protected Map<String, Object> prepareItemCatsGet(
			ItemCatsGetRequest itemCatsGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.PARENT_CID, itemCatsGetRequest.getParentCid());
		params.put(ApiConstants.CIDS, itemCatsGetRequest.getCids());
		return params;
	}

	protected ItemCatsResponse sendItemCatsGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMCATS_GET.getMethod(),
						sessionId, params));
		ItemCatsResponse itemCatsGetResponse = new ItemCatsResponse(response);
		// 平台访问成功
		if (itemCatsGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemCatsGetResponse.getBody());
				parseError(itemCatsGetResponse, json);
				// 淘宝访问成功
				if (itemCatsGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEM_CATS)) {
						JSONArray itemcats = rsp
								.getJSONArray(ApiConstants.ITEM_CATS);
						itemCatsGetResponse.setItemCats(TaobaoItemJSONConvert
								.convertJsonArrayToItemCatList(itemcats));
					}
					setTotalResults(rsp, itemCatsGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}

		}
		return itemCatsGetResponse;
	}

	/**
	 * 得到前台商品类目
	 * 
	 * @param itemCatsGetRequest
	 * @return ItemCatsListResponse
	 * @throws TaobaoApiException
	 */
	public ItemCatsListResponse itemCatsListGet(
			ItemCatsListGetRequest itemCatsGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemCatsGetRequest请求中的参数添加到params
		params.put(ApiConstants.PARENT_CID, itemCatsGetRequest.getParentCid());
		params.put(ApiConstants.CIDS, itemCatsGetRequest.getCids());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMCATS_LIST_GET
						.getMethod(), params));
		ItemCatsListResponse itemCatsListGetResponse = new ItemCatsListResponse(
				response);
		// 平台访问成功
		if (itemCatsListGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemCatsListGetResponse.getBody());
				parseError(itemCatsListGetResponse, json);
				// 淘宝访问成功
				if (itemCatsListGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEM_CATS)) {
						JSONArray itemcats = rsp
								.getJSONArray(ApiConstants.ITEM_CATS);
						itemCatsListGetResponse
								.setItemCats(TaobaoItemJSONConvert
										.convertJsonArrayToItemCatList(itemcats));
					}
					setTotalResults(rsp, itemCatsListGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemCatsListGetResponse;
	}

	/**
	 * 得到后台供卖家发布商品用的商品标准属性和属性值列表
	 * 
	 * @param itemPropsGetRequest
	 * @return ItemPropsResponse
	 * @throws TaobaoApiException
	 */
	public ItemPropsResponse itemPropsGet(ItemPropsRequest itemPropsGetRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemPropsGetRequest请求中的参数添加到params
		params.put(ApiConstants.CID, itemPropsGetRequest.getCid());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMPROPS_GET.getMethod(),
						sessionId, params));
		ItemPropsResponse itemPropsGetResponse = new ItemPropsResponse(response);
		// 平台访问成功
		if (itemPropsGetResponse.isSuccess()) {
			JSONObject json = null;
			try {
				json = new JSONObject(itemPropsGetResponse.getBody());
				parseError(itemPropsGetResponse, json);
				// 淘宝访问成功
				if (itemPropsGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEM_PROPS)) {
						JSONArray item_props = rsp
								.getJSONArray(ApiConstants.ITEM_PROPS);
						itemPropsGetResponse.setItemProps(TaobaoItemJSONConvert
								.convertJSONArrayToItemPropList(item_props));
					}
					setTotalResults(rsp, itemPropsGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemPropsGetResponse;
	}

	/**
	 * 得到前台展示的供买家浏览的商品属性
	 * 
	 * @param itemPropsListGetRequest
	 * @return ItemPropsResponse
	 * @throws TaobaoApiException
	 */
	public ItemPropsResponse itemPropsListGet(
			ItemPropsRequest itemPropsListGetRequest) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemPropsListGetRequest请求中的参数添加到params
		params.put(ApiConstants.CID, itemPropsListGetRequest.getCid());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMPROPS_LIST_GET
						.getMethod(), params));
		ItemPropsResponse itemPropsListGetResponse = new ItemPropsResponse(
				response);
		// 平台访问成功
		if (itemPropsListGetResponse.isSuccess()) {
			JSONObject json = null;
			try {
				json = new JSONObject(itemPropsListGetResponse.getBody());
				parseError(itemPropsListGetResponse, json);
				// 淘宝访问成功
				if (itemPropsListGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEM_PROPS)) {
						JSONArray item_props = rsp
								.getJSONArray(ApiConstants.ITEM_PROPS);
						itemPropsListGetResponse
								.setItemProps(TaobaoItemJSONConvert
										.convertJSONArrayToItemPropList(item_props));
					}
					setTotalResults(rsp, itemPropsListGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemPropsListGetResponse;
	}

	/**
	 * 得到spu
	 * 
	 * @param spuGetRequest
	 * @return SpuGetResponse
	 * @throws TaobaoApiException
	 */
	public SpuGetResponse spuGet(SpuGetRequest spuGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将spuGetRequest请求中的参数添加到params
		params.put(ApiConstants.CID, spuGetRequest.getCid());
		params.put(ApiConstants.FIELDS, spuGetRequest.getFields());
		params.put(ApiConstants.PROPS, spuGetRequest.getProps());
		UrlResponse response = this.getFetch()
				.fetch(
						this.getServiceUrl(),
						getTemplateRequest(TaobaoApiMethod.SPU_GET.getMethod(),
								params));
		SpuGetResponse spuGetResponse = new SpuGetResponse(response);
		// 平台访问成功
		if (spuGetResponse.isSuccess()) {
			JSONObject json = null;
			try {
				json = new JSONObject(spuGetResponse.getBody());
				parseError(spuGetResponse, json);
				// 淘宝访问成功
				if (spuGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SPUS)) {
						JSONArray spus = rsp.getJSONArray(ApiConstants.SPUS);
						spuGetResponse.setSpus(TaobaoSpuJSONConvert
								.convertJsonArrayToSpuList(spus));
					}
					setTotalResults(rsp, spuGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return spuGetResponse;
	}

	/**
	 * 得到后台供卖家发布商品用的单个商品属性。
	 * 
	 * @param itemPropGetRequest
	 * @return ItemPropResponse
	 * @throws TaobaoApiException
	 */
	public ItemPropResponse itemPropGet(ItemPropRequest itemPropGetRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemPropGetRequest请求中的参数添加到params
		params.put(ApiConstants.CID, itemPropGetRequest.getCid());
		params.put(ApiConstants.PID, itemPropGetRequest.getPid());
		params.put(ApiConstants.CHILD_PATH, itemPropGetRequest.getChildPath());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMPROP_GET.getMethod(),
						sessionId, params));
		ItemPropResponse itemPropGetResponse = new ItemPropResponse(response);
		// 平台访问成功
		if (itemPropGetResponse.isSuccess()) {
			JSONObject json = null;
			try {
				json = new JSONObject(itemPropGetResponse.getBody());
				parseError(itemPropGetResponse, json);
				// 淘宝访问成功
				if (itemPropGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEM_PROPS)) {
						JSONObject item_prop = rsp.getJSONArray(
								ApiConstants.ITEM_PROPS).getJSONObject(0);
						itemPropGetResponse.setItemProp(TaobaoItemJSONConvert
								.convertJsonObjectToItemProp(item_prop));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemPropGetResponse;
	}

	/**
	 * 得到前台展示的供买家浏览的商品属性。
	 * 
	 * @param itemPropListGetRequest
	 * @return ItemPropResponse
	 * @throws TaobaoApiException
	 */
	public ItemPropResponse itemPropListGet(
			ItemPropRequest itemPropListGetRequest) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemPropListGetRequest请求中的参数添加到params
		params.put(ApiConstants.CID, itemPropListGetRequest.getCid());
		params.put(ApiConstants.PID, itemPropListGetRequest.getPid());
		params.put(ApiConstants.CHILD_PATH, itemPropListGetRequest
				.getChildPath());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMPROP_LIST_GET
						.getMethod(), params));
		ItemPropResponse itemPropGetResponse = new ItemPropResponse(response);
		// 平台访问成功
		if (itemPropGetResponse.isSuccess()) {
			JSONObject json = null;
			try {
				json = new JSONObject(itemPropGetResponse.getBody());
				parseError(itemPropGetResponse, json);
				// 淘宝访问成功
				if (itemPropGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEM_PROPS)) {
						JSONObject item_prop = rsp.getJSONArray(
								ApiConstants.ITEM_PROPS).getJSONObject(0);
						itemPropGetResponse.setItemProp(TaobaoItemJSONConvert
								.convertJsonObjectToItemProp(item_prop));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemPropGetResponse;
	}

	/**
	 * 得到前台展示的店铺类目
	 * 
	 * @param itemPropListGetRequest
	 * @return ItemPropResponse
	 * @throws TaobaoApiException
	 */
	public ShopCatsListGetResponse shopCatsListGet() throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SHOPCATS_LIST_GET
						.getMethod(), params));
		ShopCatsListGetResponse shopCatsListGetResponse = new ShopCatsListGetResponse(
				response);
		// 平台访问成功
		if (shopCatsListGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(shopCatsListGetResponse.getBody());
				parseError(shopCatsListGetResponse, json);
				// 淘宝访问成功
				if (shopCatsListGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SHOP_CATS)) {
						JSONArray shopcats = rsp
								.getJSONArray(ApiConstants.SHOP_CATS);
						shopCatsListGetResponse
								.setShopCats(TaobaoShopJSONConvert
										.convertJsonArrayToShopCatList(shopcats));
					}
					setTotalResults(rsp, shopCatsListGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return shopCatsListGetResponse;
	}

	/**
	 * 得到前台展示的店铺内卖家自定义商品类目
	 * 
	 * @param sellerCatsListGetRequest
	 * @return SellerCatsListGetResponse
	 * @throws TaobaoApiException
	 */
	public SellerCatsListGetResponse sellerCatsListGet(
			SellerCatsListGetRequest sellerCatsListGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将sellerCatsListGetRequest请求中的参数添加到params
		params.put(ApiConstants.NICK, sellerCatsListGetRequest.getNick());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SELLERCATS_LIST_GET
						.getMethod(), params));
		SellerCatsListGetResponse sellerCatsListGetResponse = new SellerCatsListGetResponse(
				response);
		// 平台访问成功
		if (sellerCatsListGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(sellerCatsListGetResponse.getBody());
				parseError(sellerCatsListGetResponse, json);
				// 淘宝访问成功
				if (sellerCatsListGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SELLER_CATS)) {
						JSONArray sellercats = rsp
								.getJSONArray(ApiConstants.SELLER_CATS);
						sellerCatsListGetResponse
								.setSellerCats(TaobaoSellerJSONConvert
										.convertJsonArrayToSellerCatList(sellercats));
					}
					setTotalResults(rsp, sellerCatsListGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return sellerCatsListGetResponse;
	}

	// 产品部分

	/**
	 * 获取单个产品
	 * 
	 * @param productGetRequest
	 * @return ProductGetResponse
	 * @throws TaobaoApiException
	 */
	public ProductGetResponse productGet(ProductGetRequest productGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ProductGetRequest.FIELDS, productGetRequest.getFields());
		params.put(ProductGetRequest.PRODUCTID, productGetRequest
				.getProductId());
		params.put(ProductGetRequest.CID, productGetRequest.getCid());
		params.put(ProductGetRequest.PROPS, productGetRequest.getProps());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.PRODUCT_GET.getMethod(),
						params));
		ProductGetResponse productGetResponse = new ProductGetResponse(response);
		// 平台访问成功
		if (productGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(productGetResponse.getBody());
				parseError(productGetResponse, json);
				// 访问淘宝成功
				if (productGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ProductGetRequest.PRODUCT)) {
						JSONArray products = rsp
								.getJSONArray(ProductGetRequest.PRODUCT);
						JSONObject product = products.getJSONObject(0);
						productGetResponse.setProduct(TaobaoProductJSONConvert
								.convertJsonToProduct(product));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return productGetResponse;
	}

	/**
	 * 产品搜索
	 * 
	 * @param productSearchRequest
	 * @return ProductSearchResponse
	 * @throws TaobaoApiException
	 */
	public ProductSearchResponse productSearch(
			ProductSearchRequest productSearchRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ProductSearchRequest.FIELDS, productSearchRequest
				.getFields());
		params.put(ProductSearchRequest.Q, productSearchRequest.getQ());
		params.put(ProductSearchRequest.CID, productSearchRequest.getCid());
		params.put(ProductSearchRequest.PROPS, productSearchRequest.getProps());
		params.put(ApiConstants.PAGE_NO, productSearchRequest.getPageNo());
		params.put(ApiConstants.PAGE_SIZE, productSearchRequest.getPageSize());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.PRODUCTS_SEARCH.getMethod(),
						params));
		ProductSearchResponse productSearchResponse = new ProductSearchResponse(
				response);
		// 平台访问成功
		if (productSearchResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(productSearchResponse.getBody());
				parseError(productSearchResponse, json);
				// 访问淘宝成功
				if (productSearchResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ProductSearchRequest.PRODUCTS)) {
						JSONArray products = rsp
								.getJSONArray(ProductSearchRequest.PRODUCTS);
						productSearchResponse
								.setProduct(TaobaoProductJSONConvert
										.convertJsonArrayToProductList(products));
					}
					setTotalResults(rsp, productSearchResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return productSearchResponse;
	}

	// 交易部分

	/**
	 * 获取单条交易信息，包含支付宝交易号及实付金额
	 * 
	 * @param tradeGetRequest
	 * @return TradeGetResponse
	 * @throws TaobaoApiException
	 */
	public TradeGetResponse tradeGet(TradeGetRequest tradeGetRequest)
			throws TaobaoApiException {
		return tradeGet(tradeGetRequest, null);
	}

	public TradeGetResponse tradeGet(TradeGetRequest tradeGetRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareTradeGet(tradeGetRequest);
		return sendTradeGet(params, sessionId);
	}

	protected TradeGetResponse sendTradeGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.TRADE_GET.getMethod(),
						sessionId, params));
		TradeGetResponse tradeGetResponse = new TradeGetResponse(response);
		// 平台成功访问
		if (tradeGetResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(tradeGetResponse.getBody());
				parseError(tradeGetResponse, json);
				// 访问淘宝成功
				if (tradeGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADES)) {
						JSONArray trades = rsp
								.getJSONArray(ApiConstants.TRADES);
						JSONObject trade = trades.getJSONObject(0);
						tradeGetResponse.setTrade(TaobaoTradeJSONConvert
								.convertJsonToTrade(trade));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradeGetResponse;
	}

	protected Map<String, Object> prepareTradeGet(
			TradeGetRequest tradeGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将tradeGet请求中的参数添加到params
		params.put(ApiConstants.FIELDS, tradeGetRequest.getFields());
		params.put(ApiConstants.TID, tradeGetRequest.getTid());
		return params;
	}

	/**
	 * 搜索交易公开信息
	 * 
	 * @param tradesGetRequest
	 * @return TradesGetResponse
	 * @throws TaobaoApiException
	 */
	public TradesGetResponse tradesGet(TradesGetRequest tradesGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将tradesGetRequest请求中的参数添加到params
		params.put(ApiConstants.IID, tradesGetRequest.getIid());
		params.put(ApiConstants.FIELDS, tradesGetRequest.getFields());
		params.put(ApiConstants.SELLER_NICK, tradesGetRequest.getSellerNick());
		params.put(ApiConstants.PAGE_NO, tradesGetRequest.getPageNo());
		params.put(ApiConstants.PAGE_SIZE, tradesGetRequest.getPageSize());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.TRADES_GET.getMethod(),
						params));
		TradesGetResponse tradesGetResponse = new TradesGetResponse(response);
		// 平台访问成功
		if (tradesGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(tradesGetResponse.getBody());
				parseError(tradesGetResponse, json);
				// 淘宝访问成功
				if (tradesGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADES)) {
						JSONArray trades = rsp
								.getJSONArray(ApiConstants.TRADES);
						tradesGetResponse.setTrades(TaobaoTradeJSONConvert
								.convertJsonArrayToTradeList(trades));
					}
					setTotalResults(rsp, tradesGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradesGetResponse;
	}

	/**
	 * 搜索当前会话用户（做为买家）达成的交易记录
	 * 
	 * @param tradesGetRequest
	 * @return TradesGetResponse
	 * @throws TaobaoApiException
	 */
	public TradesGetResponse tradesBoughtGet(
			TradesBoughtGetRequest tradesRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = prepareTradesBought(tradesRequest);
		return sendTradesBoughtGet(params, sessionId);
	}

	/**
	 * 数据准备，将tradesBoughtGetRequest数据封装为Map对象返回
	 * 
	 * @param tradesBoughtGetRequest
	 * @return Map<String, Object>
	 * @throws
	 */
	protected Map<String, Object> prepareTradesBought(
			TradesBoughtGetRequest tradesBoughtGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将tradesRequest请求中的参数添加到params
		params.put(ApiConstants.FIELDS, tradesBoughtGetRequest.getFields());
		params.put(ApiConstants.PAGE_NO, tradesBoughtGetRequest.getPageNo());
		params
				.put(ApiConstants.PAGE_SIZE, tradesBoughtGetRequest
						.getPageSize());
		params.put(ApiConstants.START_CREATED, tradesBoughtGetRequest
				.getStartCreated());
		params.put(ApiConstants.END_CREATED, tradesBoughtGetRequest
				.getEndCreated());
		params.put(ApiConstants.TITLE, tradesBoughtGetRequest.getTitle());
		params.put(ApiConstants.STATUS, tradesBoughtGetRequest.getStatus());
		return params;
	}

	protected TradesGetResponse sendTradesBoughtGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.TRADES_BOUGHT_GET
						.getMethod(), sessionId, params));
		TradesGetResponse tradesBoughtGetResponse = new TradesGetResponse(
				response);
		// 平台访问成功
		if (tradesBoughtGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(tradesBoughtGetResponse.getBody());
				parseError(tradesBoughtGetResponse, json);
				// 淘宝访问成功
				if (tradesBoughtGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADES)) {
						JSONArray trades = rsp
								.getJSONArray(ApiConstants.TRADES);
						tradesBoughtGetResponse
								.setTrades(TaobaoTradeJSONConvert
										.convertJsonArrayToTradeList(trades));
					}
					setTotalResults(rsp, tradesBoughtGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradesBoughtGetResponse;
	}

	/**
	 * 搜索当前会话用户（做为卖家）已卖出的交易数据
	 * 
	 * @param tradesGetRequest
	 * @return TradesGetResponse
	 * @throws TaobaoApiException
	 */
	public TradesGetResponse tradesSoldGet(TradesSoldGetRequest tradesRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareTradesSold(tradesRequest);
		return sendTradesSoldGet(params, sessionId);
	}

	/**
	 * 数据准备，将tradesSoldGetRequest数据封装为Map对象返回
	 * 
	 * @param tradesRequest
	 * @return
	 */
	protected Map<String, Object> prepareTradesSold(
			TradesSoldGetRequest tradesRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将tradesRequest请求中的参数添加到params
		params.put(ApiConstants.FIELDS, tradesRequest.getFields());
		params.put(ApiConstants.PAGE_NO, tradesRequest.getPageNo());
		params.put(ApiConstants.PAGE_SIZE, tradesRequest.getPageSize());
		params.put(ApiConstants.START_CREATED, tradesRequest.getStartCreated());
		params.put(ApiConstants.END_CREATED, tradesRequest.getEndCreated());
		params.put(ApiConstants.TITLE, tradesRequest.getTitle());
		params.put(ApiConstants.STATUS, tradesRequest.getStatus());
		return params;
	}

	protected TradesGetResponse sendTradesSoldGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.TRADES_SOLD_GET.getMethod(),
						sessionId, params));
		TradesGetResponse tradesSoldGetResponse = new TradesGetResponse(
				response);
		// 平台访问成功
		if (tradesSoldGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(tradesSoldGetResponse.getBody());
				parseError(tradesSoldGetResponse, json);
				// 淘宝访问成功
				if (tradesSoldGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADES)) {
						JSONArray trades = rsp
								.getJSONArray(ApiConstants.TRADES);
						tradesSoldGetResponse.setTrades(TaobaoTradeJSONConvert
								.convertJsonArrayToTradeList(trades));
					}
					setTotalResults(rsp, tradesSoldGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradesSoldGetResponse;
	}

	public TradeRateAddResponse tradeRateAdd(
			TradeRateAddRequest tradeRateAddRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将tradeRateAddRequest请求中的参数添加到params
		params.put(ApiConstants.TID, tradeRateAddRequest.getTid());
		params.put(ApiConstants.ROLE, tradeRateAddRequest.getRole());
		params.put(ApiConstants.ANONY, tradeRateAddRequest.getAnony());
		params.put(ApiConstants.CONTENT, tradeRateAddRequest.getContent());
		params.put(ApiConstants.RESULT, tradeRateAddRequest.getResult());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.TRADERATE_ADD.getMethod(),
						sessionId, params));
		TradeRateAddResponse tradeRateAddResponse = new TradeRateAddResponse(
				response);
		// 平台成功访问
		if (tradeRateAddResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(tradeRateAddResponse.getBody());
				parseError(tradeRateAddResponse, json);
				// 访问淘宝成功
				if (tradeRateAddResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADERATES)) {
						JSONArray tradeRates = rsp
								.getJSONArray(ApiConstants.TRADERATES);
						JSONObject tradeRate = tradeRates.getJSONObject(0);
						try {
							tradeRateAddResponse.setCreated(DateUtil
									.strToDate(tradeRate
											.getString(ApiConstants.CREATED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						tradeRateAddResponse.setTid(tradeRate
								.getString(ApiConstants.TID));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradeRateAddResponse;
	}

	public TradeRatesGetResponse tradeRatesGet(
			TradeRatesGetRequest tradeRatesGetRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将tradeRatesGetRequest请求中的参数添加到params
		params.put(ApiConstants.FIELDS, tradeRatesGetRequest.getFields());
		params
				.put(ApiConstants.RATED_NICK, tradeRatesGetRequest
						.getRatedNick());
		params.put(ApiConstants.ROLE, tradeRatesGetRequest.getRole());
		params.put(ApiConstants.RATE_TYPE, tradeRatesGetRequest.getRateType());
		params.put(ApiConstants.TIME_PERIOD, tradeRatesGetRequest
				.getTimePeriod());
		params.put(ApiConstants.RESULT, tradeRatesGetRequest.getResult());
		params.put(ApiConstants.PAGE_NO, tradeRatesGetRequest.getPageNo());
		params.put(ApiConstants.PAGE_SIZE, tradeRatesGetRequest.getPageSize());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.TRADERATES_GET.getMethod(),
						sessionId, params));
		TradeRatesGetResponse tradeRatesGetResponse = new TradeRatesGetResponse(
				response);
		// 平台访问成功
		if (tradeRatesGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(tradeRatesGetResponse.getBody());
				parseError(tradeRatesGetResponse, json);
				// 访问淘宝成功
				if (tradeRatesGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADERATES)) {
						JSONArray tradeRates = rsp
								.getJSONArray(ApiConstants.TRADERATES);
						tradeRatesGetResponse
								.setTradeRates(TaobaoTradeJSONConvert
										.convertJsonArrayToTradeRateList(tradeRates));
					}
					setTotalResults(rsp, tradeRatesGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradeRatesGetResponse;
	}

	// 用户部分

	/**
	 * 获取单个用户信息
	 * 
	 * @param userGetRequest
	 * @return UserGetResponse
	 * @throws TaobaoApiException
	 */
	public UserGetResponse userGet(UserGetRequest userGetRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareUserGet(userGetRequest);
		return sendUserGet(params, sessionId);
	}

	protected Map<String, Object> prepareUserGet(UserGetRequest userGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将userGetRequest请求中的参数添加到params
		params.put(ApiConstants.FIELDS, userGetRequest.getFields());
		params.put(ApiConstants.NICK, userGetRequest.getNick());
		return params;
	}

	protected UserGetResponse sendUserGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.USER_GET.getMethod(),
						sessionId, params));
		UserGetResponse userGetResponse = new UserGetResponse(response);
		// 平台访问成功
		if (userGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(userGetResponse.getBody());
				parseError(userGetResponse, json);
				// 淘宝访问成功
				if (userGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.USERS)) {
						JSONArray users = rsp.getJSONArray(ApiConstants.USERS);
						JSONObject user = users.getJSONObject(0);
						userGetResponse.setUser(TaobaoUserJSONConvert
								.convertJsonToUser(user));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return userGetResponse;
	}

	public UserGetResponse userGet(UserGetRequest userGetRequest)
			throws TaobaoApiException {
		return this.userGet(userGetRequest, null);
	}

	/**
	 * 搜索用户信息
	 * 
	 * @param usersGetRequest
	 * @return UsersGetResponse
	 * @throws TaobaoApiException
	 */
	public UsersGetResponse usersGet(UsersGetRequest usersGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将usersGetRequest请求中的参数添加到params
		params.put(ApiConstants.FIELDS, usersGetRequest.getFields());
		params.put(ApiConstants.NICKS, usersGetRequest.getNicks());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.USERS_GET.getMethod(),
						params));
		UsersGetResponse usersGetResponse = new UsersGetResponse(response);
		// 平台访问成功
		if (usersGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(usersGetResponse.getBody());
				parseError(usersGetResponse, json);
				// 淘宝访问成功
				if (usersGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.USERS)) {
						JSONArray users = rsp.getJSONArray(ApiConstants.USERS);
						usersGetResponse.setUsers(TaobaoUserJSONConvert
								.convertJsonArrayToUserList(users));
					}
					setTotalResults(rsp, usersGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return usersGetResponse;
	}

	// 店铺部分

	/**
	 * 此接口用于获取卖家店铺基本信息
	 * 
	 * @param usersGetRequest
	 * @return UsersGetResponse
	 * @throws TaobaoApiException
	 */
	public ShopGetResponse shopGet(ShopGetRequest shopGetRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareShopGet(shopGetRequest);
		return sendShopGet(params, sessionId);
	}

	public ShopGetResponse shopGet(ShopGetRequest shopGetRequest)
			throws TaobaoApiException {
		return this.shopGet(shopGetRequest, null);
	}

	protected Map<String, Object> prepareShopGet(ShopGetRequest shopGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将shopGetRequest请求中的参数添加到params
		params.put(ApiConstants.NICK, shopGetRequest.getNick());
		params.put(ApiConstants.FIELDS, shopGetRequest.getFields());
		return params;
	}

	protected ShopGetResponse sendShopGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SHOP_GET.getMethod(),
						sessionId, params));
		ShopGetResponse shopGetResponse = new ShopGetResponse(response);
		// 平台访问成功
		if (shopGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(shopGetResponse.getBody());
				parseError(shopGetResponse, json);
				// 淘宝访问成功
				if (shopGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SHOPS)) {
						JSONArray shops = rsp.getJSONArray(ApiConstants.SHOPS);
						JSONObject shop = shops.getJSONObject(0);
						shopGetResponse.setShop(TaobaoShopJSONConvert
								.convertJsonToShop(shop));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return shopGetResponse;
	}

	/**
	 * 更新店铺的标题、公告及介绍
	 * 
	 * @param shopUpdateRequest
	 * @return ShopUpdateResponse
	 * @throws TaobaoApiException
	 */
	public ShopUpdateResponse shopUpdate(ShopUpdateRequest shopUpdateRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将shopUpdateRequest请求中的参数添加到params
		params.put(ApiConstants.TITLE, shopUpdateRequest.getTitle());
		params.put(ApiConstants.DESC, shopUpdateRequest.getDesc());
		params.put(ApiConstants.BULLETIN, shopUpdateRequest.getBulletin());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SHOP_UPDATE.getMethod(),
						sessionId, params));
		ShopUpdateResponse shopUpdateResponse = new ShopUpdateResponse(response);
		// 平台成功访问
		if (shopUpdateResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(shopUpdateResponse.getBody());
				parseError(shopUpdateResponse, json);
				// 访问淘宝成功
				if (shopUpdateResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SHOPS)) {
						JSONArray shops = rsp.getJSONArray(ApiConstants.SHOPS);
						JSONObject shop = shops.getJSONObject(0);
						shopUpdateResponse.setShop(TaobaoShopJSONConvert
								.convertJsonToShop(shop));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return shopUpdateResponse;
	}

	/**
	 * 获取剩余的橱窗推荐数量
	 * 
	 * @param shopShowcaseRemainCountRequest
	 * @return ShopShowcaseRemainCountRequest
	 * @throws TaobaoApiException
	 */
	public ShopShowcaseRemainCountResponse shopShowcaseRemainCount(
			ShopShowcaseRemainCountRequest shopShowcaseRemainCountRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SHOP_SHOWCASE_REMAINCOUNT
						.getMethod(), sessionId, params));
		ShopShowcaseRemainCountResponse shopShowcaseRemainCountResponse = new ShopShowcaseRemainCountResponse(
				response);
		// 平台成功访问
		if (shopShowcaseRemainCountResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(
						shopShowcaseRemainCountResponse.getBody());
				parseError(shopShowcaseRemainCountResponse, json);
				// 访问淘宝成功
				if (shopShowcaseRemainCountResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SHOPS)) {
						JSONArray shops = rsp.getJSONArray(ApiConstants.SHOPS);
						JSONObject shop = shops.getJSONObject(0);
						try {
							shopShowcaseRemainCountResponse
									.setRemainShowcase(Integer
											.parseInt(shop
													.getString(ApiConstants.REMAIN_COUNT)));
						} catch (JSONException e) {
							throw new TaobaoApiException(e);
						}
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return shopShowcaseRemainCountResponse;
	}

	// ********************物流部分********************************

	/**
	 * 此接口方法用于批量查询物流订单（卖家身份）
	 * 
	 * @param shippingsSendGetRequest
	 * @return ShippingsSendGetResponse
	 * @throws TaobaoApiException
	 */
	public ShippingsSendGetResponse shippingsSendGet(
			ShippingsSendGetRequest shippingsSendGetRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将shippingsSendGetRequest请求中的参数添加到params
		params.put(ApiConstants.FIELDS, shippingsSendGetRequest.getFields());
		params.put(ApiConstants.BUYER_NICK, shippingsSendGetRequest
				.getBuyerNick());
		params.put(ApiConstants.STATUS, shippingsSendGetRequest.getStatus());
		params.put(ApiConstants.RECEIVER_NAME, shippingsSendGetRequest
				.getReceiverName());
		params.put(ApiConstants.START_CREATED, shippingsSendGetRequest
				.getStartCreated());
		params.put(ApiConstants.END_CREATED, shippingsSendGetRequest
				.getEndCreated());
		params.put(ApiConstants.ITEM_TITLE, shippingsSendGetRequest
				.getItemTitle());
		params.put(ApiConstants.FREIGHT_PAYER, shippingsSendGetRequest
				.getFreightPayer());
		params.put(ApiConstants.TYPE, shippingsSendGetRequest.getType());
		params.put(ApiConstants.PAGE_NO, shippingsSendGetRequest.getPageNo());
		params.put(ApiConstants.PAGE_SIZE, shippingsSendGetRequest
				.getPageSize());
		params.put(ApiConstants.SELLER_CONFIRM, shippingsSendGetRequest
				.getSellerConfirm());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SHIPPINGS_SEND_GET
						.getMethod(), sessionId, params));
		ShippingsSendGetResponse shippingsSendGetResponse = new ShippingsSendGetResponse(
				response);
		// 平台访问成功
		if (shippingsSendGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(shippingsSendGetResponse.getBody());
				parseError(shippingsSendGetResponse, json);
				// 访问淘宝成功
				if (shippingsSendGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SHIPPINGS)) {
						JSONArray shippings = rsp
								.getJSONArray(ApiConstants.SHIPPINGS);
						shippingsSendGetResponse
								.setShippings(TaobaoShippingJSONConvert
										.convertJsonArrayToShippingList(shippings));
					}
					setTotalResults(rsp, shippingsSendGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return shippingsSendGetResponse;
	}

	/**
	 * 邮费
	 * 
	 * @param postageGetRequest
	 * @return PostagesGetRequest
	 * @throws TaobaoApiException
	 */
	public PostagesGetResponse postagesGet(
			PostagesGetRequest postageGetRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = preparePostageGet(postageGetRequest);
		return sendPostagesGet(params, sessionId);
	}

	protected Map<String, Object> preparePostageGet(
			PostagesGetRequest postageGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将postageGetRequest请求中的参数添加到params
		params.put(ApiConstants.FIELDS, postageGetRequest.getFields());
		return params;
	}

	protected PostagesGetResponse sendPostagesGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.POSTAGES_GET.getMethod(),
						sessionId, params));
		PostagesGetResponse postagesGetResponse = new PostagesGetResponse(
				response);
		if (postagesGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(postagesGetResponse.getBody());
				parseError(postagesGetResponse, json);
				// 访问淘宝成功
				if (postagesGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.POSTAGES)) {
						JSONArray postages = rsp
								.getJSONArray(ApiConstants.POSTAGES);
						postagesGetResponse
								.setPostages(TaobaoPostageJSONConvert
										.convertJsonArrayToPostageList(postages));
					}
					setTotalResults(rsp, postagesGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return postagesGetResponse;
	}

	// *******************库存部分*******************************

	/**
	 * 添加库存
	 * 
	 * @param postageGetRequest
	 * @return PostagesGetRequest
	 * @throws TaobaoApiException
	 */
	public SkuAddResponse skuAdd(SkuAddRequest skuAddRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = prepareSkuAdd(skuAddRequest);
		return sendSkuAdd(params, sessionId);
	}

	protected Map<String, Object> prepareSkuAdd(SkuAddRequest skuAddRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.IID, skuAddRequest.getIid());
		params.put(ApiConstants.PROPERTIES, skuAddRequest.getProperties());
		params.put(ApiConstants.QUANTITY, skuAddRequest.getQuantity());
		params.put(ApiConstants.PRICE, skuAddRequest.getPrice());
		params.put(ApiConstants.OUTER_ID, skuAddRequest.getOuterId());
		params.put(ApiConstants.LANG, skuAddRequest.getLang());
		return params;
	}

	protected SkuAddResponse sendSkuAdd(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_SKU_ADD.getMethod(),
						sessionId, params));
		SkuAddResponse skuAddResponse = new SkuAddResponse(response);
		if (skuAddResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(skuAddResponse.getBody());
				parseError(skuAddResponse, json);
				if (skuAddResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SKUS)) {
						JSONArray skus = rsp.getJSONArray(ApiConstants.SKUS);
						JSONObject sku = skus.getJSONObject(0);
						Sku addSku = new Sku();
						addSku.setIid(sku.getString(ApiConstants.IID));
						try {
							addSku.setCreated(DateUtil.strToDate(sku
									.getString(ApiConstants.CREATED)));
						} catch (ParseException e) {
							e.printStackTrace();
						}
						skuAddResponse.setSku(addSku);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return skuAddResponse;
	}

	/**
	 * 获取库存量
	 * 
	 * @param postageGetRequest
	 * @return PostagesGetRequest
	 * @throws TaobaoApiException
	 */
	public SkuGetResponse skuGet(SkuGetRequest skuGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = prepareSkuGet(skuGetRequest);
		return sendSkuGet(params);
	}

	protected Map<String, Object> prepareSkuGet(SkuGetRequest skuGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.SKU_ID, skuGetRequest.getSkuId());
		params.put(ApiConstants.NICK, skuGetRequest.getNick());
		params.put(ApiConstants.FIELDS, skuGetRequest.getFields());
		return params;
	}

	protected SkuGetResponse sendSkuGet(Map<String, Object> params)
			throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_SKU_GET.getMethod(),
						params));
		SkuGetResponse skuGetResponse = new SkuGetResponse(response);
		if (skuGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(skuGetResponse.getBody());
				parseError(skuGetResponse, json);
				if (skuGetResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SKUS)) {
						JSONArray skus = rsp.getJSONArray(ApiConstants.SKUS);
						JSONObject sku = skus.getJSONObject(0);
						skuGetResponse.setSku(TaobaoSkuJSONConvert
								.convertJsonToSku(sku));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return skuGetResponse;
	}

	/**
	 * 更新库存信息
	 * 
	 * @param skuUpdateRequest
	 * @return SkuUpdateResponse
	 * @throws TaobaoApiException
	 */
	public SkuUpdateResponse skuUpdate(SkuUpdateRequest skuUpdateRequest,
			String sessionId) throws TaobaoApiException {
		Map<String, Object> params = prepareSkuUpdate(skuUpdateRequest);
		return sendSkuUpdate(params, sessionId);
	}

	protected Map<String, Object> prepareSkuUpdate(
			SkuUpdateRequest skuUpdateRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.IID, skuUpdateRequest.getIid());
		params.put(ApiConstants.PROPERTIES, skuUpdateRequest.getProperties());
		params.put(ApiConstants.QUANTITY, skuUpdateRequest.getQuantity());
		params.put(ApiConstants.PRICE, skuUpdateRequest.getPrice());
		params.put(ApiConstants.OUTER_ID, skuUpdateRequest.getOuterId());
		params.put(ApiConstants.LANG, skuUpdateRequest.getLang());
		return params;
	}

	protected SkuUpdateResponse sendSkuUpdate(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_SKU_UPDATE.getMethod(),
						sessionId, params));
		SkuUpdateResponse skuUpdateResponse = new SkuUpdateResponse(response);
		if (skuUpdateResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(skuUpdateResponse.getBody());
				parseError(skuUpdateResponse, json);
				if (skuUpdateResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SKUS)) {
						JSONArray skus = rsp.getJSONArray(ApiConstants.SKUS);
						JSONObject sku = skus.getJSONObject(0);
						Sku updatedSku = new Sku();
						updatedSku.setIid(sku.getString(ApiConstants.IID));
						try {
							updatedSku.setModified(DateUtil.strToDate(sku
									.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							e.printStackTrace();
						}
						skuUpdateResponse.setSku(updatedSku);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return skuUpdateResponse;
	}

	/**
	 * 批量获取库存量
	 * 
	 * @param postageGetRequest
	 * @return PostagesGetRequest
	 * @throws TaobaoApiException
	 */
	public SkusGetResponse skusGet(SkusGetRequest skusGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = prepareSkuUpdate(skusGetRequest);
		return sendSkusGet(params);
	}

	protected Map<String, Object> prepareSkuUpdate(SkusGetRequest skusGetRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.IIDS, skusGetRequest.getIids());
		params.put(ApiConstants.FIELDS, skusGetRequest.getFields());
		params.put(ApiConstants.NICK, skusGetRequest.getNick());
		return params;
	}

	protected SkusGetResponse sendSkusGet(Map<String, Object> params)
			throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_SKUS_GET.getMethod(),
						params));
		SkusGetResponse skusGetResponse = new SkusGetResponse(response);
		if (skusGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(skusGetResponse.getBody());
				parseError(skusGetResponse, json);
				if (skusGetResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SKUS)) {
						JSONArray skus = rsp.getJSONArray(ApiConstants.SKUS);
						skusGetResponse.setSkus(TaobaoSkuJSONConvert
								.convertJsonArrayToSkuList(skus));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return skusGetResponse;
	}

	

	

	public ItemImgUploadResponse itemImgUpload(
			ItemImgUploadRequest itemImgUploadRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemImgUpload(itemImgUploadRequest);
		return sendItemImgUpload(itemImgUploadRequest, params, sessionId);
	}

	protected Map<String, Object> prepareItemImgUpload(
			ItemImgUploadRequest itemImgUploadRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params
				.put(ApiConstants.ITEMIMG_ID, itemImgUploadRequest
						.getItemImgId());
		params.put(ApiConstants.IID, itemImgUploadRequest.getIid());
		params.put(ApiConstants.POSITION, itemImgUploadRequest.getPosition());
		params.put(ApiConstants.ISMAJOR, itemImgUploadRequest.isMajor());
		return params;
	}

	protected ItemImgUploadResponse sendItemImgUpload(
			ItemImgUploadRequest itemImgUploadRequest,
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		UrlResponse response = null;
		ItemImgUploadResponse itemImgUploadResponse = null;
		if (itemImgUploadRequest.getImage() != null) {
			response = this.getFetch().fetchWithFile(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.ITEM_IMG_UPLOAD
							.getMethod(), sessionId, params),
					itemImgUploadRequest.getImage());
		} else {
			response = this.getFetch().fetch(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.ITEM_IMG_UPLOAD
							.getMethod(), sessionId, params));
		}
		itemImgUploadResponse = new ItemImgUploadResponse(response);
		if (itemImgUploadResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemImgUploadResponse.getBody());
				parseError(itemImgUploadResponse, json);
				if (itemImgUploadResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMIMGS)) {
						JSONArray itemImgs = rsp
								.getJSONArray(ApiConstants.ITEMIMGS);
						JSONObject itemImg = itemImgs.getJSONObject(0);
						ItemImg uploadItemImg = new ItemImg();
						uploadItemImg.setItemimgId(itemImg
								.getString(ApiConstants.ITEMIMG_ID));
						uploadItemImg.setUrl(itemImg
								.getString(ApiConstants.URL));
						uploadItemImg.setCreated(itemImg
								.getString(ApiConstants.CREATED));
						itemImgUploadResponse.setItemImg(uploadItemImg);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemImgUploadResponse;
	}

	public ItemImgDeleteResponse itemImgDelete(
			ItemImgDeleteRequest itemImgDeleteRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemImgDelete(itemImgDeleteRequest);
		return sendItemImgDelete(params, sessionId);
	}

	protected Map<String, Object> prepareItemImgDelete(
			ItemImgDeleteRequest itemImgDeleteRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params
				.put(ApiConstants.ITEMIMG_ID, itemImgDeleteRequest
						.getItemImgId());
		params.put(ApiConstants.IID, itemImgDeleteRequest.getIid());
		return params;
	}

	protected ItemImgDeleteResponse sendItemImgDelete(
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_IMG_DELETE.getMethod(),
						sessionId, params));
		ItemImgDeleteResponse itemImgDeleteResponse = new ItemImgDeleteResponse(
				response);
		if (itemImgDeleteResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemImgDeleteResponse.getBody());
				parseError(itemImgDeleteResponse, json);
				if (itemImgDeleteResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMIMGS)) {
						JSONArray itemImgs = rsp
								.getJSONArray(ApiConstants.ITEMIMGS);
						JSONObject itemImg = itemImgs.getJSONObject(0);
						ItemImg deleteItemImg = new ItemImg();
						deleteItemImg.setItemimgId(itemImg
								.getString(ApiConstants.ITEMIMG_ID));
						deleteItemImg.setCreated(itemImg
								.getString(ApiConstants.CREATED));
						itemImgDeleteResponse.setItemImg(deleteItemImg);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemImgDeleteResponse;
	}

	public PropimgUploadResponse propImgUpload(
			PropimgUploadRequest propimgUploadRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = preparePropImgUpload(propimgUploadRequest);
		return sendPropImgUpload(propimgUploadRequest, params, sessionId);
	}

	protected Map<String, Object> preparePropImgUpload(
			PropimgUploadRequest propimgUploadRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params
				.put(ApiConstants.PROPIMG_ID, propimgUploadRequest
						.getPropImgId());
		params.put(ApiConstants.IID, propimgUploadRequest.getIid());
		params.put(ApiConstants.PROPERTIES, propimgUploadRequest
				.getProperties());
		params.put(ApiConstants.POSITION, propimgUploadRequest.getPosition());
		return params;
	}

	protected PropimgUploadResponse sendPropImgUpload(
			PropimgUploadRequest propimgUploadRequest,
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		UrlResponse response = null;
		PropimgUploadResponse propimgUploadResponse = null;
		if (propimgUploadRequest.getImage() != null) {
			response = this.getFetch().fetchWithFile(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.ITEM_PROPIMG_UPLOAD
							.getMethod(), sessionId, params),
					propimgUploadRequest.getImage());
		} else {
			response = this.getFetch().fetch(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.ITEM_PROPIMG_UPLOAD
							.getMethod(), sessionId, params));
		}
		propimgUploadResponse = new PropimgUploadResponse(response);
		if (propimgUploadResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(propimgUploadResponse.getBody());
				parseError(propimgUploadResponse, json);
				if (propimgUploadResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.PROPIMGS)) {
						JSONArray propimgs = rsp
								.getJSONArray(ApiConstants.PROPIMGS);
						JSONObject propimg = propimgs.getJSONObject(0);
						PropImg uploadPropImg = new PropImg();
						uploadPropImg.setPropimg_id(propimg
								.getString(ApiConstants.PROPIMG_ID));
						uploadPropImg.setUrl(propimg
								.getString(ApiConstants.URL));
						uploadPropImg.setCreated(propimg
								.getString(ApiConstants.CREATED));
						propimgUploadResponse.setPropImg(uploadPropImg);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return propimgUploadResponse;
	}

	public PropImgDeleteResponse propImgDelete(
			PropImgDeleteRequest propImgDeleteRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = preparePropImgDelete(propImgDeleteRequest);
		return sendPropImgDelete(params, sessionId);
	}

	protected Map<String, Object> preparePropImgDelete(
			PropImgDeleteRequest propImgDeleteRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params
				.put(ApiConstants.PROPIMG_ID, propImgDeleteRequest
						.getPropimgId());
		params.put(ApiConstants.IID, propImgDeleteRequest.getIid());
		return params;
	}

	protected PropImgDeleteResponse sendPropImgDelete(
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_PROPIMG_DELETE
						.getMethod(), sessionId, params));
		PropImgDeleteResponse propimgDeleteResponse = new PropImgDeleteResponse(
				response);
		if (propimgDeleteResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(propimgDeleteResponse.getBody());
				parseError(propimgDeleteResponse, json);
				if (propimgDeleteResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.PROPIMGS)) {
						JSONArray propimgs = rsp
								.getJSONArray(ApiConstants.PROPIMGS);
						JSONObject propimg = propimgs.getJSONObject(0);
						PropImg deletePropimg = new PropImg();
						deletePropimg.setPropimg_id(propimg
								.getString(ApiConstants.PROPIMG_ID));
						deletePropimg.setCreated(propimg
								.getString(ApiConstants.CREATED));
						propimgDeleteResponse.setPropImg(deletePropimg);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return propimgDeleteResponse;
	}

	public OrdersGetResponse ordersGet(OrdersGetRequest ordersGetRequest)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将tradesGetRequest请求中的参数添加到params
		params.put(ApiConstants.IID, ordersGetRequest.getIid());
		params.put(ApiConstants.FIELDS, ordersGetRequest.getFields());
		params.put(ApiConstants.SELLER_NICK, ordersGetRequest.getSellerNick());
		params.put(ApiConstants.PAGE_NO, ordersGetRequest.getPageNo());
		params.put(ApiConstants.PAGE_SIZE, ordersGetRequest.getPageSize());
		UrlResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ORDERS_GET.getMethod(),
						params));
		OrdersGetResponse ordersGetResponse = new OrdersGetResponse(response);
		// 平台访问成功
		if (ordersGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(ordersGetResponse.getBody());
				parseError(ordersGetResponse, json);
				// 淘宝访问成功
				if (ordersGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ORDERS)) {
						JSONArray orders = rsp
								.getJSONArray(ApiConstants.ORDERS);
						ordersGetResponse.setOrders(TaobaoTradeJSONConvert
								.convertJsonToOrderList(orders));
					}
					setTotalResults(rsp, ordersGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return ordersGetResponse;
	}

}